package indi.zhifa.recipe.bailan.busy.service.impl;

import indi.zhifa.recipe.bailan.busy.appproperty.PlayerProperty;
import indi.zhifa.recipe.bailan.busy.entity.dto.PlayerDto;
import indi.zhifa.recipe.bailan.busy.entity.enums.EWeapon;
import indi.zhifa.recipe.bailan.busy.entity.po.PlayerEntity;
import indi.zhifa.recipe.bailan.busy.service.IPlayerService;
import indi.zhifa.recipe.bailan.framework.common.entity.exception.ServiceException;
import indi.zhifa.recipe.bailan.framework.common.util.SnowflakeIdWorker;
import indi.zhifa.recipe.bailan.framework.web.common.util.DtoEntityUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.*;

@Component
public class PlayerServiceImpl implements IPlayerService {

    Long mId;
    Map<Long,PlayerEntity> mPlayerEntityIdMap;
    Map<String,PlayerEntity> mPlayerEntityNameMap;

    public PlayerServiceImpl(){
        mId = 0L;
        mPlayerEntityIdMap = new HashMap<>();
        mPlayerEntityNameMap = new HashMap<>();
    }

    @Override
    public void init(List<PlayerProperty> pIniPlayers) {
        for(PlayerProperty playerProperty : pIniPlayers){
            PlayerEntity playerEntity = new PlayerEntity();
            playerEntity.setId(getId());
            playerEntity.setCreateTime(LocalDateTime.now());
            playerEntity.setName(playerProperty.getName());
            playerEntity.setWeapon(EWeapon.valueOf(playerProperty.getWeapon()));
            playerEntity.setDesc(playerProperty.getDesc());
            addPlayer(playerEntity);
        }
    }

    @Override
    public PlayerEntity create(PlayerDto pPlayerDto) {
        if(mPlayerEntityNameMap.containsKey(pPlayerDto.getName())){
            throw new ServiceException("已经存在名为"+pPlayerDto.getName()+"的角色");
        }
        PlayerEntity playerEntity = DtoEntityUtil.trans(pPlayerDto,PlayerEntity.class);
        playerEntity.setId(getId());
        playerEntity.setCreateTime(LocalDateTime.now());
        addPlayer(playerEntity);
        return playerEntity;
    }

    @Override
    public PlayerEntity edit(Long pId, PlayerDto pPlayerDto) {
        PlayerEntity playerEntity = checkPlayer(pId);
        DtoEntityUtil.copy(playerEntity,pPlayerDto);
        return playerEntity;
    }

    @Override
    public boolean delete(Long pId) {
        PlayerEntity playerEntity = checkPlayer(pId);
        mPlayerEntityIdMap.remove(playerEntity.getId());
        mPlayerEntityNameMap.remove(playerEntity.getName());
        return true;
    }

    @Override
    public PlayerEntity info(Long pId) {
        PlayerEntity playerEntity = checkPlayer(pId);
        return playerEntity;
    }

    @Override
    public List<PlayerEntity> list(String pName, EWeapon pWeapon) {
        if(StringUtils.hasText(pName)){
            PlayerEntity playerEntity = checkPlayer(pName);
            return Arrays.asList(playerEntity);
        }
        List<PlayerEntity> playerEntityList = new ArrayList<>(mPlayerEntityIdMap.size());
        for(PlayerEntity playerEntity : mPlayerEntityIdMap.values()){
            if(null != pWeapon){
                if(playerEntity.getWeapon() == pWeapon){
                    playerEntityList.add(playerEntity);
                }
            }else{
                playerEntityList.add(playerEntity);
            }
        }
        return playerEntityList;
    }

    protected Long getId(){
        synchronized (this.mId){
            ++mId;
            return mId;
        }
    }

    protected void addPlayer(PlayerEntity pPlayer){
        mPlayerEntityIdMap.put(pPlayer.getId(),pPlayer);
        mPlayerEntityNameMap.put(pPlayer.getName(),pPlayer);
    }

    protected PlayerEntity checkPlayer(Long pId){
        PlayerEntity playerEntity = mPlayerEntityIdMap.get(pId);
        if(null == playerEntity){
            throw new ServiceException("没有找到Id为"+pId+"的角色");
        }
        return playerEntity;
    }

    protected PlayerEntity checkPlayer(String pName){
        PlayerEntity playerEntity = mPlayerEntityNameMap.get(pName);
        if(null == playerEntity){
            throw new ServiceException("没有找到Name为"+pName+"的角色");
        }
        return playerEntity;
    }
}
